public class QuickSort {

    static int[] number;

    public static void main(String[] args) {
        number = new int[]{2,-10,2,10,11};
        int length = number.length;
        quickSort(0, length - 1);
        for (int num : number) {
            System.out.println(num);
        }
    }

    public static void quickSort(int start, int end) {
        if (end - start < 1)
            return;
        if (end - start == 1) {
            int temp;
            if (number[start] > number[end]) {
                temp = number[start];
                number[start] = number[end];
                number[end] = temp;
            }
            return;
        }

        int mid = (start + end) / 2;
        int left = start;
        int right = end;
        int levelValue;
        int levelIndex;
        if (number[start] >= number[mid] && number[start] >= number[end]) {
            if (number[mid] > number[end]) {
                levelValue = number[mid];
                levelIndex = mid;
            } else {
                levelValue = number[end];
                levelIndex = end;
            }
        } else if (number[mid] >= number[start] && number[mid] >= number[end]) {
            if (number[start] > number[end]) {
                levelValue = number[start];
                levelIndex = start;
            } else {
                levelValue = number[end];
                levelIndex = end;
            }
        } else {
            if (number[start] > number[mid]) {
                levelValue = number[start];
                levelIndex = start;
            } else {
                levelValue = number[mid];
                levelIndex = mid;
            }
        }

        boolean rightTurn = true;
        while (left <= right) {
            if(left==right&&right==levelIndex)
                break;
            if (rightTurn) {
                if(right==levelIndex){
                    rightTurn=false;
                    continue;
                }
                if (number[right] < levelValue) {
                    number[levelIndex] = number[right];
                    levelIndex = right;
                    number[right] = levelValue;
                    rightTurn = false;
                }
                right--;
            } else {
                if(left==levelIndex){
                    rightTurn=true;
                    continue;
                }
                if (number[left] > levelValue) {
                    number[levelIndex] = number[left];
                    levelIndex = left;
                    number[left] = levelValue;
                    rightTurn = true;
                }
                left++;
            }
        }
        quickSort(start, levelIndex-1);
        quickSort(levelIndex + 1, end);
    }
}
